from fastapi import APIRouter, Depends, Security
from fastapi.responses import JSONResponse

from myProfile.models.data.myProfile_models import Login, FMH
from myProfile.repository.family_medical_history import FMH_Repository
from myProfile.models.request.myProfile_request import FMHReq
from myProfile.db_config.odmantic_config import create_db_engine
from myProfile.security.secure import get_current_valid_user

router = APIRouter()


@router.post("/fmh/add")
async def add_fmh(req: FMHReq,
                  current_user: Login = Security(
                      get_current_valid_user, scopes=["user"]),
                  engine=Depends(create_db_engine)):
    fmh = FMH(**req.model_dump())
    repo: FMH_Repository = FMH_Repository(engine)
    result = await repo.add_to_profile(current_user.id, fmh)
    if result is True:
        return req
    else:
        return JSONResponse(
            content={"message": "add fmh problem encountered."},
            status_code=500)


@router.delete("/fmh/delete/{id}")
async def delete_fmh(id: int,
                     current_user: Login = Security(
                         get_current_valid_user, scopes=["user"]),
                     engine=Depends(create_db_engine)):
    repo: FMH_Repository = FMH_Repository(engine)
    result = await repo.remove_from_profile(current_user.id, id)
    if result is True:
        return JSONResponse(
            content={"message": "delete fmh successfully"},
            status_code=201)
    else:
        return JSONResponse(
            content={"message": "delete fmh unsuccessfully"},
            status_code=500)


@router.get("/fmh/get")
async def get_fmh(current_user: Login = Security(
                    get_current_valid_user, scopes=["user"]),
                  engine=Depends(create_db_engine)):
    repo: FMH_Repository = FMH_Repository(engine)
    return await repo.get_from_profile(current_user.id)
